/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_SSAP_CLIENT_HDI
#define SLE_SSAP_CLIENT_HDI

#include <stdint.h>
#include <stdbool.h>
#include "sle_hal_errcode.h"
#include "sle_ssap_config.h"
#include "sle_ssap_client_config.h"

#ifdef _Cplusplus
extern "C" {
#endif

/**
 * @brief   Callback invoked when service discovery.
 * 
 * @param   [in] clientId client ID.
 * @param   [in] connectId   connection ID.
 * @param   [in] service   service information.
 * @param   [in] errCode    error code.
 * @return  None
 */
typedef void (*SleHdiSsapcFindStructureCallback)(uint8_t clientId, uint16_t connectId,
    const ClientFindServiceRes *Service, SleHdiErrorCode errCode);

/**
 * @brief  Callback invoked when property discovered.
 * 
 * @param   [in] clientId client ID.
 * @param   [in] connectId   connection ID.
 * @param   [in] property  property.
 * @param   [in] errCode    error code.
 * @return  None
 */
typedef void (*SleHdiSsapcFindPropertyCallback)(uint8_t clientId, uint16_t connectId,
    const ClientFindPropertyRes *property, SleHdiErrorCode errCode);

/**
 * @brief   Callback invoked when find completed.
 * 
 * @param   [in] clientId        client ID.
 * @param   [in] connectId          connection ID.
 * @param   [in] structureResult input parameter.
 * @param   [in] errCode           error code.
 * @return None
 */
typedef void (*SleHdiSleHdiSsapcFindStructureCompleteCallback)(uint8_t clientId, uint16_t connectId,
    const ClientFindStructureRes *StructureResult, SleHdiErrorCode errCode);

/**
 * @brief   Callback invoked when receive read response.
 * 
 * @param   [in] clientId client ID.
 * @param   [in] connectId   connection ID.
 * @param   [in] readData read data.
 * @param   [in] errCode    error code.
 * @return None
 */
typedef void (*SleHdiSsapcReadConformCallback)(uint8_t clientId, uint16_t connectId, const ClientHandleValue *readData,
    SleHdiErrorCode errCode);

/**
 * @brief Callback invoked when read feature completed.
 * 
 * @param [in] clientId  client ID.
 * @param [in] connectId    connection ID.
 * @param [in] cmpResult input parameter.
 * @param [in] errCode     error code.
 * @return None
 */
typedef void (*SleHdiSsapcReadByUuidCompleteCallback)(uint8_t clientId, uint16_t connectId,
    const ClientReadByUuidCmpRes *cmpResult, SleHdiErrorCode errCode);

/**
 * @brief Callback function for receiving write response.
 * 
 * @param [in] clientId    client ID.
 * @param [in] connectId      connection ID.
 * @param [in] writeResult write result.
 * @param [in] errCode       error code.
 * @return None
 */
typedef void (*SleHdiSsapcWriteConformCallback)(uint8_t clientId, uint16_t connectId, const ClientWriteRes *writeResult,
    SleHdiErrorCode errCode);

/**
 * @brief Callback invoked when receive exchange info response.
 * 
 * @param [in] clientId client ID.
 * @param [in] connectId   connection ID.
 * @param [in] param     exchange info.
 * @param [in] errCode    error code.
 * @return None
 */
typedef void (*SleHdiSsapcExchangeInfoCallback)(uint8_t clientId, uint16_t connectId, const SsapMtuInfo *param,
    SleHdiErrorCode errCode);

/**
 * @brief Callback function for receiving a notification.
 * 
 * @param [in] clientId client ID.
 * @param [in] connectId   connection ID.
 * @param [in] data      data.
 * @param [in] errCode    error code.
 * @return None
 */
typedef void (*SleHdiSsapcNotificationCallback)(uint8_t clientId, uint16_t connectId, const ClientHandleValue *data,
    SleHdiErrorCode errCode);

/**
 * @brief Callback function for receiving a indication.
 * 
 * @param [in] clientId client ID.
 * @param [in] connectId   connection ID.
 * @param [in] data      data.
 * @param [in] errCode    error code.
 * @return None
 */
typedef void (*SleHdiSsapcIndicationCallback)(uint8_t clientId, uint16_t connectId, const ClientHandleValue *data,
    SleHdiErrorCode errCode);

/**
 * @brief Struct of Ssap client callback function.
 */
typedef struct {
    SleHdiSsapcFindStructureCallback OnStructureFoundCb;              /*!< @if Eng Discovery structure callback.
                                                                       @else   发现服务回调函数。 @endif */
    SleHdiSsapcFindPropertyCallback OnPropertyFoundCb;               /*!< @if Eng Discovery property callback.
                                                                       @else   发现特征回调函数。 @endif */
    SleHdiSleHdiSsapcFindStructureCompleteCallback OnFindStructureCmpCb; /*!< @if Eng Discovery structure complete callback.
                                                                       @else   发现特征完成回调函数。 @endif */
    SleHdiSsapcReadConformCallback OnRecvReadResponseCb;               /*!< @if Eng Receive write response callback.
                                                                       @else   收到读响应回调函数。 @endif */
    SleHdiSsapcReadByUuidCompleteCallback OnRreadByUuidCmpCb;     /*!< @if Eng Callback hook for read property complete.
                                                                       @else   读特征值完成回调钩子。 @endif */
    SleHdiSsapcWriteConformCallback OnRecvWriteResponseCb;              /*!< @if Eng Receive write response callback.
                                                                       @else   收到写响应回调函数。 @endif */
    SleHdiSsapcExchangeInfoCallback OnExchangeInfoCb;                /*!< @if Eng Callback hook for configure mtu size complete.
                                                                       @else   更新mtu大小回调钩子。 @endif */
    SleHdiSsapcNotificationCallback OnRecvNotificationCb;                  /*!< @if Eng Callback hook for receive notification.
                                                                       @else   通知事件上报钩子。 @endif */
    SleHdiSsapcIndicationCallback OnRecvIndicationCb;                      /*!< @if Eng Callback hook for receive indication.
                                                                       @else   指示事件上报钩子。 @endif */
} SleSsapcHdiCallbacks;

/**
 * @brief  Register Ssap client.
 * 
 * @param  [in]  appUuid  App uuid
 * @param  [out] clientId Client ID
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiSsapcRegisterClient)(const SleUuid *appUuid, uint8_t *clientId);

/**
 * @brief  Unregister Ssap client.
 * 
 * @param  [in] clientId Client ID
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiSsapcUnregisterClient)(uint8_t clientId);

/**
 * @brief  Find structure.
 * 
 * @param [in] clientId Client ID.
 * @param [in] connectId   Connection ID.
 * @param [in] param     structure uuid, if NULL, discover all structure, else filter by uuid.
 * @return error code
 */
typedef SleHdiErrorCode (*SleHdiSsapcFindStructure)(uint8_t clientId, uint16_t connectId, const ClientFindStructure *param);

/**
 * @brief  Send read by uuid request.
 * 
 * @param [in] clientId Client ID.
 * @param [in] connectId   Connection ID.
 * @param [in] param     Parameter for read request by uuid.
 * @return error code
*/
typedef SleHdiErrorCode (*SleHdiSsapcReadReqByUuid)(uint8_t clientId, uint16_t connectId, const ClientReadReqByUUID *param);

 /**
 * @brief  Send read by handle request.
 * 
 * @param [in] clientId Client ID.
 * @param [in] connectId   Connection ID.
 * @param [in] handle    handle.
 * @param [in] type      property type.
 * @return error code
 */
typedef SleHdiErrorCode (*SleHdiSsapcReadReq)(uint8_t clientId, uint16_t connectId, uint16_t handle, uint8_t type);

/**
 * @brief  Send write request.

 * @param [in] clientId Client ID.
 * @param [in] connectId   Connection ID.
 * @param [in] param     Parameter for write request.
 */
typedef SleHdiErrorCode (*SleHdiSsapcWriteReq)(uint8_t clientId, uint16_t connectId, const ClientWriteParam *param);

/**
 * @brief  Send write command.
 * 
 * @param [in] clientId Client ID.
 * @param [in] connectId   Connection ID.
 * @param [in] param     Parameter for write command.
 * @return error code.

 * @endif
 */
typedef SleHdiErrorCode (*SleHdiSsapcWriteCmd)(uint8_t clientId, uint16_t connectId, const ClientWriteParam *param);

/**
 * @brief  Send exchange info request.
 * 
 * @param [in] clientId Client ID.
 * @param [in] connectId   Connection ID.
 * @param [in] param     Client info.
 * @return error code
 */
typedef SleHdiErrorCode (*SleHdiSsapcExchangeInfoReq)(uint8_t clientId, uint16_t connectId, const SsapMtuInfo* param);

/**
 * @brief  Register SSAP client callbacks.
 * 
 * @param [in] func Callback function.
 * @return error code.
 */
typedef SleHdiErrorCode (*SleHdiRegisterSsapcCallbacks)(SleSsapcHdiCallbacks *ssapcCallbacks);

typedef struct {
    SleHdiSsapcRegisterClient         registerClient;
    SleHdiSsapcUnregisterClient       unregisterClient;
    SleHdiSsapcFindStructure          findStructure;
    SleHdiSsapcReadReqByUuid          readReqByUuid;
    SleHdiSsapcReadReq                readReq;
    SleHdiSsapcWriteReq               writeReq;
    SleHdiSsapcWriteCmd               writeCmd;
    SleHdiSsapcExchangeInfoReq        exchangeInfoReq;
    SleHdiRegisterSsapcCallbacks      registerClientCbks;
} SleSsapcHdiOpts;

/**
 * @brief  Get Ssapc client options.
 * 
 * @param [out] ssapcOpts Ssapc client options.
 * @return error code.
 */
SleHdiErrorCode GetSleSsapcHdiOpts(SleSsapcHdiOpts *ssapcOpts);

#ifdef _Cplusplus
}
#endif
#endif /* SLE_SSAP_CLIENT_HDI */
